package cn.pcManage.goodsManage.service.impl;

import cn.common.common.model.ServerResponse;
import cn.common.goods.model.BaseGoods;
import cn.common.goods.model.BaseGoodsType;
import cn.pcManage.goodsManage.model.Goods;
import cn.pcManage.goodsManage.model.GoodsViews;
import cn.pcManage.goodsManage.service.IGoodsTypeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import cn.pcManage.goodsManage.dao.GoodsMapper;
import cn.pcManage.goodsManage.dao.GoodsTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Zhang on 2019/1/11.
 */
@Service
public class GoodsTypeServiceImpl implements IGoodsTypeService {
    @Autowired
    GoodsTypeMapper goodsTypeMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public ServerResponse<?> getTypeList(BaseGoodsType goodsType) {
        Map<String,Object> resultMap=new HashMap<String, Object>(2);
        PageHelper.startPage(goodsType.getPage(),goodsType.getLimit());
        List<BaseGoodsType> goodsTypeList = goodsTypeMapper.getTypeList(goodsType);
        Page<BaseGoodsType> goodsTypePage = (Page<BaseGoodsType>) goodsTypeList;
        resultMap.put("total", goodsTypePage.getTotal());
        resultMap.put("infos", goodsTypeList);
        return ServerResponse.createBySuccess(resultMap);
    }

    @Override
    public ServerResponse<?> deleteType(Integer id) {
        List<BaseGoodsType> goodsTypeList = goodsTypeMapper.getTypeListByFatherId(id);
        if (goodsTypeList.size() > 0){
            return ServerResponse.createByErrorMessage("请先删除子类目");
        }
        BaseGoods goods = new BaseGoods();
        goods.setTypeId(id);
        List<GoodsViews> goodsList = goodsMapper.getGoodsList(goods);
        if (goodsList.size() > 0){
            return ServerResponse.createByErrorMessage("请先删除类目下商品");
        }
        goodsTypeMapper.deleteByPrimaryKey(id);
        return ServerResponse.createBySuccessMessage("删除成功");
    }

    @Override
    public ServerResponse<?> backToFatherList(Integer id) {
        BaseGoodsType goodsType = goodsTypeMapper.selectByPrimaryKey(id);
        if (goodsType.getFatherId() == null){
            return ServerResponse.createByErrorMessage("已是一级目录");
        }else {
            Map<String,Object> resultMap=new HashMap<String, Object>(2);
            goodsType = goodsTypeMapper.selectByPrimaryKey(goodsType.getFatherId());
            PageHelper.startPage(1,10);
            List<BaseGoodsType> goodsTypeList = goodsTypeMapper.getTypeListByFatherId(goodsType.getFatherId());
            Page<BaseGoodsType> goodsTypePage = (Page<BaseGoodsType>) goodsTypeList;
            resultMap.put("total", goodsTypePage.getTotal());
            resultMap.put("infos", goodsTypeList);
            return ServerResponse.createBySuccess(resultMap);
        }
    }

    @Override
    public ServerResponse<?> addType(BaseGoodsType goodsType) {
        if (goodsType.getFatherId()!=null){
            BaseGoods goods = new BaseGoods();
            goods.setTypeId(goodsType.getFatherId());
            List<GoodsViews> goodsList = goodsMapper.getGoodsList(goods);
            if (goodsList.size() > 0){
                return ServerResponse.createByErrorMessage("请先删除当前类目下商品");
            }
        }
        goodsTypeMapper.insertSelective(goodsType);
        return ServerResponse.createBySuccessMessage("新增成功");
    }

    @Override
    public ServerResponse<?> getTypeSelect() {
        return ServerResponse.createBySuccess(goodsTypeMapper.getTypeSelect());
    }

    @Override
    public ServerResponse<?> updateType(BaseGoodsType goodsType) {
        goodsTypeMapper.updateByPrimaryKeySelective(goodsType);
        return ServerResponse.createBySuccessMessage("更新成功");
    }
}
